package com.hzdy.master.config;

import org.hibernate.integrator.spi.Integrator;
import org.hibernate.jpa.boot.spi.IntegratorProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMysql",   //EntityManagerFactory引用
        transactionManagerRef = "transactionManagerMysql",      //transactionManager引用
        basePackages = {"com.hzdy.master.repository"})

public class MysqlDataSourceConfig {
    /**
     * 注入 mysql数据源
     */
    @Resource(name = "mysqlDataSource")
    private DataSource mysqlDataSource;
    /**
     * 注入JPA配置实体
     */
    @Autowired
    private JpaProperties jpaProperties;

    /**
     * 配置EntityManagerFactory实体
     *
     * @return 实体管理工厂
     * packages     扫描@Entity注释的软件包名称
     * persistenceUnit  持久性单元的名称。 如果只建立一个EntityManagerFactory，你可以省略这个，但是如果在同一个应用程序中有多个，你应该给它们不同的名字
     * properties       标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
     */
    @Primary
    @Bean("entityManagerFactoryMysql")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(){
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);
        vendorAdapter.setDatabase(Database.MYSQL);
        LocalContainerEntityManagerFactoryBean factoryBean =new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(mysqlDataSource);
        factoryBean.setPersistenceUnitName("mysqlPersistenceUnit");
        factoryBean.setPersistenceUnitPostProcessors(new ReflectionsPersistenceUnitPostProcessor());
        factoryBean.setPackagesToScan("com.hzdy.master.entity");
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        Properties jpaProperties = new Properties();
        Integrator integrator = new MetadataExtractorIntegrator();
        if (integrator != null) {
            jpaProperties.put("hibernate.integrator_provider",
                    (IntegratorProvider) () -> Collections.singletonList(
                            MetadataExtractorIntegrator.INSTANCE
                    )
            );
        }
        factoryBean.setJpaProperties(jpaProperties);
        return factoryBean;
    }


    /**
     * 配置EntityManager实体
     *
     * @return 实体管理器
     */
    @Primary
    @Bean(name = "entityManagerMysql")
    public EntityManager entityManager() {
        return entityManagerFactoryMysql().getObject().createEntityManager();
    }


    /**
     * 配置事务transactionManager
     *
     * @return 事务管理器
     */
    @Primary
    @Bean(name = "transactionManagerMysql")
    public PlatformTransactionManager transactionManagerMysql() {
        return new JpaTransactionManager(entityManagerFactoryMysql().getObject());
    }

}